<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img class="logo" src="../assets/logo.png" alt="" />
        <span>Data Manage System</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 左边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏 -->
        <el-menu
          background-color="#32c5b7"
          text-color="#fff"
          active-text-color="#ffd04b" :unique-opened="true" :collapse="isCollapse"
          :collapse-transition="false" :router="true" :default-active="defaultActive">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id.toString()" v-for="item in menuList" :key="item.id">
              <!-- 一级菜单的模板区 -->
            <template slot="title">
                <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path.toString()" v-for="subItem in item.children" :key="subItem.id" @click="changeActive(subItem.path)">
                <!-- 图标 -->
                <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>{{subItem.authName}}</span>
                </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'icon iconfont icon-manager',
        103: 'icon iconfont icon-storage',
        101: 'icon iconfont icon-dingdan',
        102: 'icon iconfont icon-networks',
        145: 'icon iconfont icon-log_report'
      },
      // 是否折叠
      isCollapse: false,
      // 默认高亮users
      defaultActive: '/rights'
    }
  },
  created () {
    this.getMenuList()
    this.defaultActive = window.sessionStorage.getItem('activePath')
  },
  methods: {
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }

      this.menuList = res.data
    },
    logout() {
      window.sessionStorage.clear()
      this.$router.replace('/login') // push
    },
    // 菜单的折叠和展开
    toggleCollapse() {
      console.log(this.isCollapse)
      this.isCollapse = !this.isCollapse
    },
    changeActive(path) {
      console.log('/' + path)
      window.sessionStorage.setItem('activePath', '/' + path)
      this.defaultActive = '/' + path
    }
    // handleOpen(key, keyPath) {
    //   console.log(key, keyPath)
    // },
    // handleClose(key, keyPath) {
    //   console.log(key, keyPath)
    // }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.logo {
  height: 40px;
}
.el-header {
  background-color: #2ac2d1;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #32c5b7;
  .el-menu {
    border-right:none;
  }
}
.el-main {
  background-color: #e8e8e8;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #32c5b7;
  font-size: 10px;
  line-height: 24px;
  cursor: pointer;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
}
</style>
